<?php
// 本类由系统自动生成，仅供测试用途
namespace Home\Controller;
use Think\Controller;
class FriendsController extends Controller {

	public function addFriends()
    {
        $tel = I('request.tel');
        $mobilephone = I('request.mobilephone');
        $user = M('user');
        $res_user = $user -> where('tel='.$tel) -> find();
        $res_admin = null;
        if ($tel == $mobilephone) {
            $this -> ajaxReturn(['msg' => '不能添加自己为好友'], 'JSONP');
        }
        if (!$res_user) {
            $admin = M('admin');
            $res_admin = $admin -> where('tel='.$tel) -> find();
            if (!$res_admin) {
                $this -> ajaxReturn(['msg' => '没有此联系人'], 'JSONP');
            }
        }
        $friends = M('friends');
        $res = $friends -> where('utel='.$mobilephone.' AND ftel='.$tel) -> find();
        if ($res) {
            $this -> ajaxReturn(['msg' => '已经是好友,无需再添加'], 'JSONP');
        }
        $res_friends = $friends -> add(['utel' => $mobilephone, 'ftel' => $tel]);
        $res_f = $friends -> add(['utel' => $tel, 'ftel' => $mobilephone]);
        if ($res_friends && $res_f) {
            $this -> ajaxReturn(['msg' => '添加成功'], 'JSONP');
        } else {
            $this -> ajaxReturn(['msg' => '添加失败,请重新添加'], 'JSONP');
        }
	}

	public function deleteFriends()
    {
        $utel = I('request.utel');
        $ftel = I('request.ftel');
        $friends = M('friends');
        $result = $friends -> where('utel='.$utel.' AND ftel='.$ftel) -> delete();
        $result2 = $friends -> where('utel='.$ftel.' AND ftel='.$utel) -> delete();

        $result3 = $friends -> field('jdc_user.tel, jdc_user.nickname, jdc_user.head_url')
            -> join('jdc_user on jdc_user.tel=jdc_friends.ftel')
            -> where('jdc_friends.utel='.$utel) -> select();

        $res_admin = $friends -> field('jdc_admin.tel, jdc_admin.name as nickname')
            -> join('jdc_admin on jdc_admin.tel=jdc_friends.ftel')
            -> where('jdc_friends.utel='.$utel) -> select();


        foreach ($res_admin as $k => $v) {
            array_push($result3, $res_admin[$k]);
        }
        if ($result && $result2) {
            $this -> ajaxReturn(['msg' => 1, 'result' => $result3], 'JSONP');
        } else {
            $this -> ajaxReturn(['msg' => 0, 'sql'=> $friends->getLastSql()], 'JSONP');
        }
    }

    public function sendMsg()
    {
        $message = I('request.msg');
        $utel = I('request.utel');
        $ftel = I('request.ftel');

        $data = [
            'utel' => $utel,
            'ftel' => $ftel,
            'msg' => $message,
            'create_time' => time(),
            'status' => 0
        ];

        $path1 = 'Public/Uploads/logs/'.$utel.'-'.$ftel.'.json';
        $path2 = 'Public/Uploads/logs/'.$ftel.'-'.$utel.'.json';
        $result = null;
        if (doesObjectExist($path1)) {
            $result = ossGetObject($path1);
            if (!is_array(json_decode($result))) {
                $json_data = json_decode('['.$result.']');
            } else {
                $json_data = json_decode($result);
            }
            array_push($json_data,$data);
            $result = ossPutObject($path1, json_encode($json_data));
        } else {
            if (doesObjectExist($path2)) {
                $result = ossGetObject($path2);
                if (!is_array(json_decode($result))) {
                    $json_data = json_decode('['.$result.']');
                } else {
                    $json_data = json_decode($result);
                }
                array_push($json_data,$data);
                $result = ossPutObject($path2, json_encode($json_data));
            } else {
                $result = ossPutObject($path1, json_encode($data));
            }

        }

        if ($result) {
            $this -> ajaxReturn(['msg' => 1], 'JSONP');
        } else {
            $this -> ajaxReturn(['msg' => 0], 'JSONP');
        }

    }

    public function SeeMsg()
    {
        $utel = I('request.utel');
        $ftel = I('request.ftel');

        $path1 = 'Public/Uploads/logs/'.$utel.'-'.$ftel.'.json';
        $path2 = 'Public/Uploads/logs/'.$ftel.'-'.$utel.'.json';
        $path3 = null;
        $result = null;
        if (doesObjectExist($path1)) {
            $path3 = $path1;
            $result = ossGetObject($path1);
        } else {
            if (doesObjectExist($path2)) {
                $path3 = $path2;
                $result = ossGetObject($path2);
            } else {
                $this -> ajaxReturn([], 'JSONP');
            }
        }
        if (!is_array(json_decode($result))) {
            $result = '['.$result.']';
        }
        $content = json_decode($result, true);
        $content2 = [];
        foreach ($content as $k => $v) {
            if ($content[$k]['ftel'] == $utel && $content[$k]['status'] == 0) {
                $content[$k]['status'] = 1;
                array_push($content2,$content[$k]);
            }
        }
        ossPutObject($path3, json_encode($content));
        $this -> ajaxReturn(['content' => $content2, 'content2' => $content], 'JSONP');
    }

    public function getUnreadMsg()
    {
        $tel = I('request.tel');
        $friends = M('friends');
        $res_friends = $friends -> field('utel, ftel') -> where('utel='.$tel) -> select();
        $result = [];
        foreach ($res_friends as $k => $v) {
            $path1 = 'Public/Uploads/logs/'.$tel.'-'.$res_friends[$k]['ftel'].'.json';
            $path2 = 'Public/Uploads/logs/'.$res_friends[$k]['ftel'].'-'.$tel.'.json';
            $content = null;
            if (doesObjectExist($path1)) {
                $content = ossGetObject($path1);
            } else {
                if (doesObjectExist($path2)) {
                    $content = ossGetObject($path2);
                }
            }
            $result[$res_friends[$k]['ftel']] = 0;
            if (!is_array(json_decode($content))) {
                $content = '['.$content.']';
            }
            $content2 = json_decode($content, true);
            foreach ($content2 as $key => $value) {
                if ($content2[$key]['ftel'] == $tel && $content2[$key]['status'] == 0) {
                    $result[$res_friends[$k]['ftel']] += 1;
                }
            }
        }
        $this -> ajaxReturn($result, 'JSONP');

    }

    public function getSendMsg()
    {
         $utel = I('request.utel');
         $ftel = I('request.ftel');
         $path1 = 'Public/Uploads/logs/'.$utel.'-'.$ftel.'.json';
         $path2 = 'Public/Uploads/logs/'.$ftel.'-'.$utel.'.json';
         $result = null;
         $path3 = null;
         if (doesObjectExist($path1)) {
             $path3 = $path1;
             $result = ossGetObject($path1);
         } else {
             if (doesObjectExist($path2)) {
                 $path3 = $path2;
                 $result = ossGetObject($path2);
             } else {
                 $this -> ajaxReturn([], 'JSONP');
             }
         }
         if (!is_array(json_decode($result))) {
             $result = '['.$result.']';
         }
        $content = json_decode($result, true);
        $result = [];
        foreach ($content as $k => $v) {
            if ($content[$k]['ftel'] == $ftel && $content[$k]['status'] == 0) {
                array_push($result, $content[$k]);
                $content[$k]['status'] = 1;
            }
        }
        ossPutObject($path3, json_encode($content));
        $this -> ajaxReturn($result, 'JSONP');

    }

    public function getFriendsInfo()
    {
        $ftel = I('request.ftel');
        $user = M('user');
        $result = $user -> where('tel='.$ftel) -> find();
        if ($result) {
            $this -> ajaxReturn($result, 'JSONP');
        } else {
            $admin = M('admin');
            $result = $admin -> where('tel='.$ftel) -> find();
            if ($result) {
                $result['nickname'] = $result['name'];
                $this -> ajaxReturn($result, 'JSONP');
            }
        }
    }

    public function getFriends()
    {
        $utel = I('request.mobilephone');
        $friends = M('friends');

        $result = $friends -> field('jdc_user.tel, jdc_user.nickname, jdc_user.head_url')
            -> join('jdc_user on jdc_user.tel=jdc_friends.ftel')
            -> where('jdc_friends.utel='.$utel) -> select();

        $res_admin = $friends -> field('jdc_admin.tel, jdc_admin.name as nickname')
                            -> join('jdc_admin on jdc_admin.tel=jdc_friends.ftel')
                            -> where('jdc_friends.utel='.$utel) -> select();

        foreach ($result as $k => $v) {
            foreach ($res_admin as $j => $v) {
                if ($result[$k]['tel'] == $res_admin[$j]['tel']) {
                    $res_admin[$j] = null;
                }
            }
        }

        foreach ($res_admin as $k => $v) {
            if ($res_admin[$k] != null) {
                $res_admin[$k]['head_url'] = null;
                array_push($result, $res_admin[$k]);
            }
        }

        $this -> ajaxReturn($result,'JSONP');
    }

}